next up previous
Next: 3.4 Careful Using Variable Up: 3 Generic 64-bit Portability Previous: 3.2 Bad Assumptions about

3.3 Poorly Sized Malloc Calls

Using standard C library routines is also prone to errors arising from invalid assumptions. The malloc memory allocation routine is a good example. While a bad practice, the value passed to malloc's allocation size parameter may be based on implicit type size assumptions. For example:

#include <stdlib.h>
long *list20;
list20 = (long*) malloc(20*4);
instead of:
list20 =
  (long*) malloc(20 * sizeof(long));
Compilers are unlikely to report such problems. You can find these hard-to-find problems by inspection of the code and the use of a debugging malloc package during testing. Other routines requiring byte extent lengths like bcopy, bzero, memset, memcpy, and memmove are subject to the same problem.



next up previous
Next: 3.4 Careful Using Variable Up: 3 Generic 64-bit Portability Previous: 3.2 Bad Assumptions about



Mark Kilgard
Sat Dec 30 11:52:07 PST 1995